// ==================================================================
// @(#)export_ntf.c
//
// @author Bruno Quoitin (bruno.quoitin@uclouvain.be)
// @date 15/10/07
// $Id: export_ntf.c,v 1.5 2009-08-31 09:46:42 bqu Exp $
//
// C-BGP, BGP Routing Solver
// Copyright (C) 2002-2008 Bruno Quoitin
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
// 02111-1307  USA
// ==================================================================

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */

#include <net/error.h>
#include <net/network.h>
#include <net/link-list.h>
#include <net/node.h>
#include <net/subnet.h>

// -----[ net_export_ntf ]-------------------------------------------
int net_export_ntf(gds_stream_t * stream, network_t * network)
{
  gds_enum_t * nodes;
  net_node_t * node;
  net_iface_t * iface;
  unsigned int index;

  // Header
  stream_printf(stream, "#\n");
  stream_printf(stream, "# Network topology NTF file\n");
  stream_printf(stream, "# generated by C-BGP\n");
  stream_printf(stream, "#\n");
  stream_printf(stream, "# Formats:\n");
  stream_printf(stream, "# <RID node 1> -- <RID node 2> -- <weight>\n");
  stream_printf(stream, "# <RID node 1> <IP iface 1> <RID node 2> <IP iface 2> <weight>\n");
  stream_printf(stream, "# <RID node> <IP iface> <ID subnet> -- <weight>\n");
  stream_printf(stream, "#\n");

  // Links
  nodes= trie_get_enum(network->nodes);
  while (enum_has_next(nodes)) {
    node= *((net_node_t **) enum_get_next(nodes));
    for (index= 0; index < net_ifaces_size(node->ifaces); index++) {
      iface= (net_iface_t *) node->ifaces->data[index];
      switch (iface->type) {
      case NET_IFACE_RTR:
	node_dump_id(stream, node);
	stream_printf(stream, "\t---\t");
	node_dump_id(stream, iface->dest.iface->owner);
	stream_printf(stream, "\t---\t%u\n", net_iface_get_metric(iface, 0));
	break;
      case NET_IFACE_PTP:
	node_dump_id(stream, node);
	stream_printf(stream, "\t");
	net_iface_dump_id(stream, iface);
	stream_printf(stream, "\t");
	node_dump_id(stream, iface->dest.iface->owner);
	stream_printf(stream, "\t");
	net_iface_dump_id(stream, iface->dest.iface);
	stream_printf(stream, "\t%u\n", net_iface_get_metric(iface, 0));
	break;
      case NET_IFACE_PTMP:
	node_dump_id(stream, node);
	stream_printf(stream, "\t");
	net_iface_dump_id(stream, iface);
	stream_printf(stream, "\t");
	subnet_dump_id(stream, iface->dest.subnet);
	stream_printf(stream, "\t--\t%u\n", net_iface_get_metric(iface, 0));
	break;
      default:
	;
      }

    }
  }
  enum_destroy(&nodes);

  return ESUCCESS;
}

